#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>

int main(void){
  printf("创建父进程%d\n",getpid());
  int son_pid = fork();
  int son_status=0; // 保存子进程状态
  if(son_pid<0){
    perror("fork");
    exit(EXIT_FAILURE);
  }
  if(son_pid==0){
    // 子进程执行
    char *path = "/usr/bin/ping";
    // 参数数组
    char *arvg[]={"ping","-c","50","www.baidu.com",NULL};
    char *envp[]={NULL};
    int res = execve(path,arvg,envp);
    if(res==-1){
      printf("子进程调用ping失败");
    }
  }else{
    // 父进程执行
    printf("父进程等待子进程执行结束。。。\n");
    // 父进程开始等待
    waitpid(son_pid,&son_status,0);    
  }
  printf("父进程%d等待子进程%d执行结束:%d\n",
    getpid(),son_pid,son_status);
  return 0;
}
